#!/bin/bash

# Sanity check script for performance testing
# Ensures UDS vs TCP comparison is fair and accurate

set -e

echo "==============================================="
echo "     Performance Sanity Check"
echo "==============================================="

# Configuration parameters - MUST be identical for both tests
TOTAL_RECORDS=1000000
BATCH_SIZE=1000
LINGER_MS=10
CONCURRENCY=4
MODE="stream"

# Build the benchmark
echo -e "\n[1/6] Building benchmark..."
cd /home/xiaodouzi/workspace/422a67685e23f903e048434351289787/build
make bench_write -j4 > /dev/null 2>&1

# Clean database before test
echo -e "\n[2/6] Cleaning database..."
PGPASSWORD=postgres psql -U postgres -h localhost -d testdb -c "TRUNCATE TABLE bench_test_0, bench_test_1, bench_test_2, bench_test_3;" 2>/dev/null || true

# Count initial records
INITIAL_COUNT=$(PGPASSWORD=postgres psql -U postgres -h localhost -d testdb -t -c "SELECT COUNT(*) FROM bench_test_0 UNION ALL SELECT COUNT(*) FROM bench_test_1 UNION ALL SELECT COUNT(*) FROM bench_test_2 UNION ALL SELECT COUNT(*) FROM bench_test_3;" | awk '{s+=$1} END {print s}')
echo "Initial record count: $INITIAL_COUNT"

# Test 1: UDS Mode
echo -e "\n[3/6] Running UDS benchmark..."
echo "Config: records=$TOTAL_RECORDS batch=$BATCH_SIZE linger=$LINGER_MS threads=$CONCURRENCY"

UDS_OUTPUT=$(./bench/cpp/bench_write \
    --mode $MODE \
    --transport uds \
    --path /tmp/kwdb.sock \
    --records $TOTAL_RECORDS \
    --batch-size $BATCH_SIZE \
    --linger-ms $LINGER_MS \
    --concurrency $CONCURRENCY \
    2>&1)

# Extract UDS QPS
UDS_QPS=$(echo "$UDS_OUTPUT" | grep "^QPS:" | awk '{print $2}')
UDS_SUCCESS=$(echo "$UDS_OUTPUT" | grep "^Writes OK:" | awk '{print $3}')
echo "UDS Results: QPS=$UDS_QPS, Success=$UDS_SUCCESS"

# Verify database count for UDS
UDS_DB_COUNT=$(PGPASSWORD=postgres psql -U postgres -h localhost -d testdb -t -c "SELECT COUNT(*) FROM bench_test_0 UNION ALL SELECT COUNT(*) FROM bench_test_1 UNION ALL SELECT COUNT(*) FROM bench_test_2 UNION ALL SELECT COUNT(*) FROM bench_test_3;" | awk '{s+=$1} END {print s}')
UDS_ACTUAL_WRITTEN=$((UDS_DB_COUNT - INITIAL_COUNT))
echo "UDS DB Verification: Expected=$TOTAL_RECORDS, Actual=$UDS_ACTUAL_WRITTEN"

# Clean database for TCP test
echo -e "\n[4/6] Cleaning database for TCP test..."
PGPASSWORD=postgres psql -U postgres -h localhost -d testdb -c "TRUNCATE TABLE bench_test_0, bench_test_1, bench_test_2, bench_test_3;" 2>/dev/null || true

# Test 2: TCP Mode
echo -e "\n[5/6] Running TCP benchmark..."
echo "Config: records=$TOTAL_RECORDS batch=$BATCH_SIZE linger=$LINGER_MS threads=$CONCURRENCY"

TCP_OUTPUT=$(./bench/cpp/bench_write \
    --mode $MODE \
    --transport tcp \
    --host 127.0.0.1 \
    --port 9009 \
    --records $TOTAL_RECORDS \
    --batch-size $BATCH_SIZE \
    --linger-ms $LINGER_MS \
    --concurrency $CONCURRENCY \
    2>&1)

# Extract TCP QPS
TCP_QPS=$(echo "$TCP_OUTPUT" | grep "^QPS:" | awk '{print $2}')
TCP_SUCCESS=$(echo "$TCP_OUTPUT" | grep "^Writes OK:" | awk '{print $3}')
echo "TCP Results: QPS=$TCP_QPS, Success=$TCP_SUCCESS"

# Verify database count for TCP
TCP_DB_COUNT=$(PGPASSWORD=postgres psql -U postgres -h localhost -d testdb -t -c "SELECT COUNT(*) FROM bench_test_0 UNION ALL SELECT COUNT(*) FROM bench_test_1 UNION ALL SELECT COUNT(*) FROM bench_test_2 UNION ALL SELECT COUNT(*) FROM bench_test_3;" | awk '{s+=$1} END {print s}')
TCP_ACTUAL_WRITTEN=$((TCP_DB_COUNT - INITIAL_COUNT))
echo "TCP DB Verification: Expected=$TOTAL_RECORDS, Actual=$TCP_ACTUAL_WRITTEN"

# Analysis
echo -e "\n[6/6] Analysis Results:"
echo "==============================================="
echo "Configuration Used:"
echo "  - Mode: $MODE"
echo "  - Total Records: $TOTAL_RECORDS"
echo "  - Batch Size: $BATCH_SIZE"
echo "  - Linger MS: $LINGER_MS"
echo "  - Concurrency: $CONCURRENCY"
echo ""
echo "UDS Performance:"
echo "  - QPS: $UDS_QPS"
echo "  - Client Success Count: $UDS_SUCCESS"
echo "  - Database Count: $UDS_ACTUAL_WRITTEN"
echo "  - Match: $([ "$UDS_SUCCESS" = "$UDS_ACTUAL_WRITTEN" ] && echo "✓" || echo "✗")"
echo ""
echo "TCP Performance:"
echo "  - QPS: $TCP_QPS"
echo "  - Client Success Count: $TCP_SUCCESS"
echo "  - Database Count: $TCP_ACTUAL_WRITTEN"
echo "  - Match: $([ "$TCP_SUCCESS" = "$TCP_ACTUAL_WRITTEN" ] && echo "✓" || echo "✗")"
echo ""

# Calculate ratio
if [ -n "$TCP_QPS" ] && [ -n "$UDS_QPS" ] && [ "$TCP_QPS" != "0" ]; then
    RATIO=$(echo "scale=2; $UDS_QPS / $TCP_QPS" | bc)
    echo "Performance Ratio (UDS/TCP): ${RATIO}x"
else
    echo "Performance Ratio: Unable to calculate"
fi

echo ""
echo "Data Integrity Check:"
if [ "$UDS_SUCCESS" = "$TOTAL_RECORDS" ] && [ "$UDS_ACTUAL_WRITTEN" = "$TOTAL_RECORDS" ]; then
    echo "  UDS: ✓ All points accounted for"
else
    echo "  UDS: ✗ Mismatch detected"
fi

if [ "$TCP_SUCCESS" = "$TOTAL_RECORDS" ] && [ "$TCP_ACTUAL_WRITTEN" = "$TOTAL_RECORDS" ]; then
    echo "  TCP: ✓ All points accounted for"
else
    echo "  TCP: ✗ Mismatch detected"
fi

echo "==============================================="